package com.example.baiguangsystem.config;

import com.example.baiguangsystem.entity.Author;
import com.example.baiguangsystem.entity.Book;
import com.example.baiguangsystem.vo.ResultType;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 将该切面切入book控制器类方法之中，对返回值之中的date类型数据进行格式化
 *
 * @Author: 27641
 * @ProjectName: BaiGuangSystem
 * @Date: 2022/02/05/12:20
 * @noinspection SpellCheckingInspection
 */
@Component
@Aspect
public class BookDateTransformAop {

    /**
     * 切入点
     */
    @Pointcut(value = "execution(* com.example.baiguangsystem.constroller.BookController.*(..))")
    public void MyPoincut() {
    }


    /**
     * 后置通知,给book类中的author作者的出生日期date转换为string类型
     *
     * @param result 目标方法的返回值
     */
    @AfterReturning(value = "MyPoincut()", returning = "result")
    public void afterReturning(Object result) {
        if ((result != null) && (result instanceof ResultType)) {
            ResultType resultPakage = (ResultType) result;
            if (resultPakage.getData() instanceof Book && resultPakage.getData() != null) {
                Book book = (Book) resultPakage.getData();
                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Author author = book.getAuthor();
                author.setAuthorDateFormat(dateFormat.format(author.getAuthorDate()));
                resultPakage.setData(book);
                resultPakage.setMsg(resultPakage.getMsg() + "==后置通知进行了封装!");
            }
        }
    }

}
